package com.example.himalaya.presenters;

import com.example.himalaya.model.api.SearchApi;
import com.example.himalaya.presenters.interfaces.IRecommendPresenter;
import com.example.himalaya.view.callback.IRecommendViewCallback;
import com.example.himalaya.constants.Constants;
import com.example.himalaya.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.constants.DTransferConstants;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.GussLikeAlbumList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendPresenter implements IRecommendPresenter {


    // Variable
    private static final String TAG = "RecommendPresenter";

    private List<IRecommendViewCallback> mCallbacks = new ArrayList<>();
    private List<Album> mCurrentRecommend = null;
    private List<Album> mAlbumList;

    private RecommendPresenter() {
    }


    private static RecommendPresenter sInstance = null;


    /**
     * 获取单例对象
     */
    public static RecommendPresenter getInstance() {
        if (sInstance == null) {
            synchronized (RecommendPresenter.class) {
                sInstance = new RecommendPresenter();
            }
        }

        return sInstance;
    }

    @Override
    public void getRecommendList() {
        // 获取推荐内容
        getRecommandData();
    }


    @Override
    public void pull2RefrehMore() {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void registerViewCallback(IRecommendViewCallback callback) {
        if (callback != null && !mCallbacks.contains(callback)) {
            mCallbacks.add(callback);
        }
    }

    @Override
    public void unRegisterViewCallback(IRecommendViewCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }


    private void getRecommandData() {

        if (mAlbumList != null && mAlbumList.size() > 0) {
            handlerRecommendResult(mAlbumList);
            return;
        }

        // 显示加载图标
        updateLoading();

        // 封装参数
        Map<String, String> map = new HashMap<>();

        // 这个参数表示一页数据返回多少条
        map.put(DTransferConstants.LIKE_COUNT, Constants.COUNT_RECOMMEND + "");

        SearchApi searchApi = SearchApi.getSearchApi();

        searchApi.getRecommendList(new IDataCallBack<GussLikeAlbumList>() {
            @Override
            public void onSuccess(GussLikeAlbumList gussLikeAlbumList) {
                LogUtil.d(TAG, "thread name -->" + Thread.currentThread().getName());

                // 数据获取成功
                if (gussLikeAlbumList != null) {
                    mAlbumList = gussLikeAlbumList.getAlbumList();

                    if (mAlbumList != null) {

                        // 数据回来了， 更新数据
                        handlerRecommendResult(mAlbumList);


                        LogUtil.d(TAG, "size ---" + mAlbumList.size());
                    }
                }
            }

            @Override
            public void onError(int i, String s) {
                // 数据获取失败
                LogUtil.d(TAG, "error --> " + i);
                LogUtil.d(TAG, "errorMsg --> " + s);
                handlerError();
            }
        }, map);
    }

    private void handlerError() {
        if (mCallbacks != null) {
            for (IRecommendViewCallback callback : mCallbacks) {
                callback.onNetworkError();
            }
        }
    }

    private void handlerRecommendResult(List<Album> albumList) {
        // 通知UI更新
        if (albumList != null) {

            if (albumList.size() == 0) {
                for (IRecommendViewCallback callback : mCallbacks) {
                    // 的到的数据为空
                    callback.onEmpty();
                }
            } else {
                for (IRecommendViewCallback callback : mCallbacks) {

                    // 正常的数据
                    callback.onRecommendListLoaded(albumList);
                }

                this.mCurrentRecommend = albumList;
            }
        }
    }

    private void updateLoading() {
        for (IRecommendViewCallback callback : mCallbacks) {
            callback.onLoading();
        }
    }

    /**
     *
     */
    public List<Album> getCurrentRecommend() {
        return mCurrentRecommend;
    }
}
